import useFrom from "@/views/workTable/compositions/useFrom";

interface IUserSrcParam {
  hash?: string
}

export const merchantDevHost = `http://${window.location.hostname}:3000`
export const sassDevHost = `http://${window.location.hostname}:3001`

export function useSassSrc({hash = ''}: IUserSrcParam) {
  const _env = import.meta.env.MODE
  const _origin = _env === 'development' ? sassDevHost : location.origin
  const _pathname = '/sass'
  const _hash = hash
  const params = new URLSearchParams()
  params.set('from', 'sass')
  const paramString = '?' + params.toString()

  return {
    origin: _origin,
    src: `${_origin}${_pathname}${_hash}${paramString}`,
  }
}

export function useMerchantSrc() {
  const _env = import.meta.env.MODE
  const _origin = _env === 'development' ? merchantDevHost : location.origin

  return {
    origin: _origin
  }
}

export function useIFrameOrigin() {
  const {origin: sassOrigin} = useSassSrc({})
  const {origin: merchantOrigin} = useMerchantSrc()
  const from = useFrom()

  switch (from) {
    case 'merchant':
      return merchantOrigin;
    case 'merchantTemplate':
      return merchantOrigin;
    case 'sass':
      return sassOrigin;
    default:
      return merchantOrigin;
  }
}

